import { getPub, makeTopicOfPub, wrapToMessage } from "../proxy/nsqProxy";

const topicOfCloseUnpayExpiredOrder = "close-unpay_expired_order";
const topicOfCloseEndedActivity = "close-ended_activity";

const topicOfSettleProfitedOrder = "settle-profited_order";
const topicOfSendActivityRemindNotification = "send-activity_remind_notification";
const topicOfSubmitOrderProfitShareAtAfterSaleDeadline = "submit-order_profit_share_at_after_sale_deadline"
const topicOfRetryNsqFail = "retry-nsq_fail"

export const closeUnpayExpiredOrders = async (limit: number) => {
  return new Promise((resolve, reject) => {
    const topic = makeTopicOfPub(topicOfCloseUnpayExpiredOrder);
    const msg = wrapToMessage({
      limit,
      invoker: "cron",
    });
    getPub().publish(topic, msg, (err) => {
      if (err) {
        reject(false);
      }
      resolve(true);
    });
  });
};

export const closeEndedActivities = async (limit: number) => {
  return new Promise((resolve, reject) => {
    const topic = makeTopicOfPub(topicOfCloseEndedActivity);
    const msg = wrapToMessage({
      limit,
      invoker: "cron",
    });
    getPub().publish(topic, msg, (err) => {
      if (err) {
        reject(false);
      }
      resolve(true);
    });
  });
};

export const settleProfitedOrders = async (limit: number) => {
  return new Promise((resolve, reject) => {
    const topic = makeTopicOfPub(topicOfSettleProfitedOrder);
    const msg = wrapToMessage({
      limit,
      invoker: "cron",
    });
    getPub().publish(topic, msg, (err) => {
      if (err) {
        reject(false);
      }
      resolve(true);
    });
  });
};

export const sendCheckIn1RemindNotifications = async (limit: number) => {
  return new Promise((resolve, reject) => {
    const topic = makeTopicOfPub(topicOfSendActivityRemindNotification);
    const msg = wrapToMessage({
      limit,
      invoker: "cron",
    });
    getPub().publish(topic, msg, (err) => {
      if (err) {
        reject(false);
      }
      resolve(true);
    });
  });
};

export const submitOrderProfitShareAtAfterSaleDeadline = async (limit: number) => {
  return new Promise((resolve, reject) => {
    const topic = makeTopicOfPub(topicOfSubmitOrderProfitShareAtAfterSaleDeadline);
    const msg = wrapToMessage({
      limit,
      invoker: "cron",
    });
    getPub().publish(topic, msg, (err) => {
      if (err) {
        reject(false);
      }
      resolve(true);
    });
  });
};

export const retryNsqFail = async (limit: number) => {
  return new Promise((resolve, reject) => {
    const topic = makeTopicOfPub(topicOfRetryNsqFail);
    const msg = wrapToMessage({
      limit,
      invoker: "cron",
    });
    getPub().publish(topic, msg, (err) => {
      if (err) {
        reject(false);
      }
      resolve(true);
    });
  });
};



